/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.mtons.mblog.web.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.mtons.mblog.base.lang.Consts;
import com.mtons.mblog.base.lang.Result;
import com.mtons.mblog.base.utils.BeanMapUtils;
import com.mtons.mblog.modules.data.CommentVO;
import com.mtons.mblog.modules.data.PostVO;
import com.mtons.mblog.modules.entity.Say;
import com.mtons.mblog.modules.service.CommentService;
import com.mtons.mblog.modules.service.PostService;
import com.mtons.mblog.modules.service.SayService;
import com.mtons.mblog.web.controller.BaseController;

import lombok.extern.slf4j.Slf4j;

/**
 * 侧边栏数据加载
 *
 * @author langhsu
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class ApiController extends BaseController {
	@Autowired
	private PostService postService;
	@Autowired
	private CommentService commentService;
	@Autowired
	private SayService sayService;

	@PostMapping(value = "/login")
	public Result login(String username, String password) {
		return executeLogin(username, password, false);
	}

	@RequestMapping(value = "/say")
	@ResponseBody
	public Result<String> say(@RequestBody Say say) {
		log.debug(say.getComment());
		sayService.say(say);
		return Result.success("记录成功！", say.getComment());
	}

	@RequestMapping(value = "/getsay")
	@ResponseBody
	public Page<Say> getsay(@RequestBody Say say) {
		log.debug(say.getComment());
		Pageable pageable = wrapPageable();
		return sayService.paging(pageable);
	}

	@RequestMapping("/posts")
	public Page<PostVO> posts(HttpServletRequest request) {
		String order = ServletRequestUtils.getStringParameter(request, "order", Consts.order.NEWEST);
		int channelId = ServletRequestUtils.getIntParameter(request, "channelId", 0);
		return postService.paging(wrapPageable(Sort.by(Sort.Direction.DESC, BeanMapUtils.postOrder(order))), channelId,
				null);
	}

	@RequestMapping(value = "/latest_comments")
	public List<CommentVO> latestComments(@RequestParam(name = "size", defaultValue = "6") Integer size) {
		return commentService.findLatestComments(size);
	}
}
